package com.surfilter.risk.service.impl;

import com.google.common.collect.Lists;
import com.surfilter.risk.mapper.MouseMovePointMapper;
import com.surfilter.risk.pojo.MouseMovePoint;
import com.surfilter.risk.service.MouseMovePointService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * MouseMovePointServiceImpl
 * Dis:
 * Created by grom on 2017/6/26.
 */
@Service("mouseMovePointService")
public class MouseMovePointServiceImpl implements MouseMovePointService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MouseMovePointMapper mouseMovePointMapper;


    @Override
    public int insertMouseMovePoint(String pointJson) {
        try {
            List<MouseMovePoint> pointList = split2point(pointJson);
            if(null != pointList && pointList.size()>0){
                for(MouseMovePoint point : pointList){
                    mouseMovePointMapper.insert(point);
                }
            }
            return 1;
        }catch (Exception e){
            logger.info("插入异常："+e.getMessage());
            return 0;
        }
    }

    private List<MouseMovePoint> split2point(String pointJson) {
        String [] pointStr = pointJson.substring(2,pointJson.length()-1).split("\\],\\[");
        List<MouseMovePoint> list = Lists.newArrayList();
        for(int i=0;i<pointStr.length;i++){
            MouseMovePoint mouseMovePoint = new MouseMovePoint();
            String [] str = pointStr[i].split(",");
            Integer xPoint = Integer.parseInt(str[0]);
            Integer yPoint = Integer.parseInt(str[1]);
            Integer tPoint = Integer.parseInt(str[3]);
            if( i == pointStr.length -1){
                mouseMovePoint.setEndDis(Integer.parseInt(str[4]));
            }
            mouseMovePoint.setOmmTimes(i+1);
            mouseMovePoint.setxPoint(xPoint);
            mouseMovePoint.setyPoint(yPoint);
            mouseMovePoint.settPoint(tPoint);
            list.add(mouseMovePoint);
        }
        return list;
    }

    public static void  main(String[] args){
        String pointJson = ",[1,1,1,11],[1,0,1,21],[0,0,1,1],[1,1,2,21],[1,0,2,11],[0,0,2,16],[1,0,2,5],[1,0,2,22],[0,0,2,23],[2,0,2,0],[1,0,2,9],[1,0,2,12],[3,0,2,10],[0,0,2,12],[1,1,3,10],[2,0,3,12],[2,1,4,11],[1,0,4,10],[0,0,4,4],[2,0,4,6],[1,0,4,11],[1,0,4,12],[2,0,4,11],[3,0,4,9],[0,0,4,2],[2,0,4,9],[2,0,4,11],[2,0,4,12],[2,0,4,10],[0,0,4,1],[2,0,4,11],[2,0,4,9],[2,0,4,11],[1,0,4,11],[0,0,4,4],[2,0,4,7],[1,0,4,11],[1,0,4,11],[1,0,4,10],[0,0,4,8],[1,0,4,3],[1,0,4,11],[1,0,4,12],[1,0,4,10],[1,0,4,11],[1,0,4,10],[1,0,4,11],[1,0,4,23],[0,0,4,2],[2,0,4,9],[2,0,4,21],[1,0,4,10],[0,0,4,9],[2,0,4,13],[1,0,4,11],[1,0,4,10],[0,0,4,12],[1,0,4,10],[2,0,4,11],[2,0,4,11],[1,0,4,11],[0,0,4,6],[2,0,4,4],[2,0,4,11],[2,0,4,23],[0,0,4,7],[2,0,4,4],[1,0,4,10],[1,0,4,10],[1,0,4,11],[0,0,4,11],[2,0,4,0],[1,0,4,11],[2,0,4,12],[1,0,4,10],[1,0,4,10],[0,0,4,4],[1,0,4,8],[1,0,4,10],[1,0,4,11],[0,0,4,17],[1,0,4,13],[1,0,4,29],[0,0,4,7],[1,0,4,12],[0,0,4,33],[1,0,4,13],[1,0,4,30],[0,0,4,5],[0,0,4,46],[1,0,4,28],[1,0,4,11],[0,0,4,7],[1,0,4,14],[0,0,4,34],[1,0,4,4],[1,1,5,19],[0,0,5,24],[1,0,5,46],[0,0,5,5],[0,0,5,41],[0,0,5,52],[1,0,5,22],[0,0,5,20],[1,0,5,34],[0,0,5,15],[1,0,5,4],[1,0,5,30],[0,0,5,11],[0,0,5,50],[1,0,5,1],[1,0,5,35],[0,0,5,6],[0,1,6,40],[0,0,6,8],[1,0,6,27],[0,0,6,22],[0,1,7,8],[1,0,7,27],[0,0,7,9],[0,0,7,46],[0,0,7,49],[1,0,7,7],[0,0,7,43],[0,0,7,47],[1,0,7,27],[0,0,7,15],[0,0,7,50],[0,0,7,45],[0,0,7,50],[0,0,7,44],[0,0,7,47],[0,0,7,46],[0,0,7,53],[0,0,7,41],[0,0,7,51],[0,0,7,43],[0,0,7,47],[0,0,7,45],[0,0,7,49],[0,0,7,29,1]";
    }
}
